// Package api
// @Time  : 2022/3/18 上午10:07
// @Author: Jtyoui@qq.com
// @note  :
package api

import (
	"fmt"
	"strings"
	"time"
)

type Data struct {
	Address string `json:"address"`
	Title   string `json:"title"`
	Content string `json:"content"`
	People  string `json:"people"`
	Time    string `json:"time"`
	Valid   bool   `json:"valid"`
	Flag    bool   `json:"flag"`
}

var Result Data

type Subscribe struct {
	MachineId     string   `json:"machineId"`
	Machine       string   `json:"machine"`
	People        string   `json:"people"`
	DateTimeRange []string `json:"dateTimeRange"`
	Address       string   `json:"content"`
}

func strToSub(timeStr string) string {
	start, err := time.Parse("2006-01-02 15:04:05", timeStr)
	if err != nil {
		panic(err)
	}
	result := fmt.Sprintf("%02d/%02d %02d:%02d", start.Month(), start.Day(), start.Hour(), start.Second())
	return result
}

func (s *Subscribe) getTime() string {
	start, end := s.DateTimeRange[0], s.DateTimeRange[1]
	rangeTime := strings.Split(start, " ")
	ls.Subscribed = strToSub(start) + " " + strToSub(end)

	for i := 0; i < len(ts); i++ {
		startHour := strings.Split(strToSub(start), " ")[1]
		endHour := strings.Split(strToSub(end), " ")[1]
		value := strings.Split(ts[i].Value, "-")
		startValue, endValue := value[0], value[1]
		if startValue <= startHour && endHour <= endValue {
			ts[i].Disabled = true
		}
	}
	return rangeTime[0]
}

func (s *Subscribe) isValid() (b bool) {
	now := time.Now()
	startDate := strings.TrimSpace(s.DateTimeRange[0])
	start, err := time.Parse("2006-01-02 15:04:05", startDate)
	endDate := strings.TrimSpace(s.DateTimeRange[1])
	end, err := time.Parse("2006-01-02 15:04:05", endDate)
	if err != nil {
		return
	}
	if now.Sub(start) < 0 || now.Sub(end) < 0 {
		b = true
	}
	return
}

type Times struct {
	Value    string `json:"value"`
	Disabled bool   `json:"disabled"`
}

var ts = make([]Times, 0, 6)

func (*Data) GetTime() []Times {
	if len(ts) == 0 {
		for i := 0; i < 12; i += 2 {
			value := fmt.Sprintf("%02d:00-%02d:00", 8+i, 10+i)
			h := time.Now().Hour()

			var disabled bool
			if h >= 8+i {
				disabled = true
			} else {
				disabled = false
			}

			t := Times{Value: value, Disabled: disabled}
			ts = append(ts, t)
		}
	}
	return ts
}
